import service from '@/utils/request'

const formatTime = (date, pattern) => {
  if (typeof date === 'string') {
    date = new Date(date.replace(/-/g, '/'))
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  if (pattern) {
    return pattern.replace('yyyy', year)
      .replace('MM', formatNumber(month))
      .replace('dd', formatNumber(day))
      .replace('hh', formatNumber(hour))
      .replace('HH', formatNumber(hour))
      .replace('mm', formatNumber(minute))
      .replace('ss', formatNumber(second))
  }
  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const parseTime = (date) => {
  if (typeof date === 'string') {
    date = new Date(date.replace(/-/g, '/'))
  }
  return date
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return `${[year, month, day].map(formatNumber).join('-')}`
}

const getBetweenDateByTitle = title => {
  const param = {}
  const now = new Date()
  if (title === '今天') {
    param.beginDate = formatDate(now) + ' 00:00:00'
    param.endDate = formatDate(now) + ' 23:59:59'
  } else if (title === '昨天') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)) + ' 23:59:59'
  } else if (title === '近7天') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate())) + ' 23:59:59'
  } else if (title === '今年') {
    param.beginDate = formatDate(new Date(now.getFullYear(), 0, 1)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), 11, 31)) + ' 23:59:59'
  } else if (title === '前天') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 2)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 2)) + ' 23:59:59'
  } else if (title === '本月') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth(), 1)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth() + 1, 0)) + ' 23:59:59'
  } else if (title === '上月') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth() - 1, 1)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth(), 0)) + ' 23:59:59'
  } else if (title === '上上月') {
    param.beginDate = formatDate(new Date(now.getFullYear(), now.getMonth() - 2, 1)) + ' 00:00:00'
    param.endDate = formatDate(new Date(now.getFullYear(), now.getMonth() - 1, 0)) + ' 23:59:59'
  }
  return param
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const getQueryParam = obj => {
  obj = obj || {}
  const arr = []
  for (const k in obj) {
    arr.push([k, obj[k] || ''].join('='))
  }
  return arr.join('&')
}

const parseQueryParam = url => {
  url = decodeURIComponent(url)
  const params = {}
  try {
    let array = []
    if (url.indexOf('?') != -1) {
      array = url.split('?')[1].split('&')
    } else {
      array = url.split('&')
    }
    for (let i = 0; i < array.length; i++) {
      const e = array[i]
      const k = e.split('=')[0]
      const v = e.split('=')[1]
      if (!params[k]) {
        params[k] = v
      }
    }
  } catch (e) {
    console.log(e)
    return {}
  }
  return params
}

const getPickerOptions = () => {
  const array = '今天,昨天,前天,近7天,本月,上月,今年'.split(',')
  const shortcuts = []
  for (const index in array) {
    shortcuts.push({
      text: array[index],
      onClick(picker) {
        const ret = getBetweenDateByTitle(array[index])
        picker.$emit('pick', [parseTime(ret.beginDate), parseTime(ret.endDate)])
      }
    })
  }
  return { shortcuts: shortcuts }
}

export default {
  getPickerOptions,
  parseTime,
  formatTime,
  formatDate,
  getQueryParam,
  parseQueryParam,
  getBetweenDateByTitle
}

